import java.util.function.Consumer;

public class LinkedList {

    public static void main(String[] args) {
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.addValue(1);
        doubleLinkedList.addValue(2);
        doubleLinkedList.addValue(3);
        doubleLinkedList.addValue(4);
        doubleLinkedList.printList(System.out::println);
    }


}

//双向链表
class DoubleLinkedList{
    //头尾指针
    Node head;
    Node tail;

    public void addValue(int value){
        //先看头节点是否为空，为空则给他创建一个节点，同时让尾节点也指向它
        if (head == null){
            head = new Node(null,value,null);
            tail = head;
        }else {
            //不为空则让尾节点的下一个指向新节点，新节点的上一个指向尾节点
            tail.next = new Node(tail,value,null);
            //给尾节点赋值，保证尾节点永远在最后一个
            tail = tail.next;
        }

    }

    public void printList(Consumer<Integer> consumer){
        //函数式接口打印
        Node p = head;
        while (p != null){
            consumer.accept(p.value);
            p = p.next;
        }
    }



    private class Node{
       private Node pre;
       private int value;
       private Node next;

        public Node(Node pre, int value, Node next) {
            this.pre = pre;
            this.value = value;
            this.next = next;
        }
    }

}
